/*
 *  Copyright 2007-2008 by Matthias Buch-Kromann <mbk.isv@cbs.dk>.
 *  *
 *  This file is part of the Open-source Dependency Toolkit (OSDT),
 *  see http://code.google.com/p/open-source-dependency-toolkit.
 *  
 *  The OSDT is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as 
 *  published by the Free Software Foundation, either version 3 of 
 *  the License, or (at your option) any later version.
 * 
 *  The OSDT is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public 
 *  License along with the OSDT in the files COPYING and COPYING.LESSER.
 *  If not, see http://www.gnu.org/licenses.
 */

package org.osdtsystem.graphs.views;

import java.util.regex.Matcher;
import org.osdtsystem.graphs.Graph;
import org.osdtsystem.graphs.io.GraphIDs;
import org.osdtsystem.graphs.io.OsdtHelper;
import org.osdtsystem.values.Layer;
import org.osdtsystem.values.NodeID;

/**
 *
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public class ViewFactory {
    public static View decodeView(String engine, String encoding, Graph graph, GraphIDs ids) {
        // Process views
        if (engine.equals("DependencyAlignmentEngine")) {
            return createDependencyAlignmentEngine(encoding, graph, ids);
        } else if (engine.equals("DependencyTreeEngine")) {
            return createDependencyTreeEngine(encoding, graph, ids);
        } else if (engine.equals("NodeCollectionEngine")) {
            return createNodeCollectionEngine(encoding, graph, ids);
        } else if (engine.equals("ProjectiveTreeEngine")) {
            return createProjectiveTreeEngine(encoding, graph, ids);
        } else if (engine.equals("TreeEngine")) {
            return createTreeEngine(encoding, graph, ids);
        } else if (engine.equals("WordAlignmentEngine")) {
            return createWordAlignmentEngine(encoding, graph, ids);
        } else {
            return null;
        }
    }


    private static View createDependencyAlignmentEngine(String encoding, Graph graph, GraphIDs ids) {
        Matcher m = OsdtHelper.osdtDependencyAlignmentEnginePattern.matcher(encoding);
        if (m.matches()) {
            DependencyTree source = (DependencyTree) ids.getView(m.group(1));
            DependencyTree target = (DependencyTree) ids.getView(m.group(2));
            WordAlignment wordAlignment = (WordAlignment) ids.getView(m.group(3));
            Layer dependencyAlignmentLayer = ids.getLayer(m.group(4));
            if (source != null && target != null && wordAlignment != null && dependencyAlignmentLayer != null)
                return new DependencyAlignmentEngine(graph, source, target, 
                        wordAlignment, dependencyAlignmentLayer);
        }
        return null;
    }

    private static View createDependencyTreeEngine(String encoding, Graph graph, GraphIDs ids) {
        Matcher m = OsdtHelper.osdtDependencyTreeEnginePattern.matcher(encoding);
        if (m.matches()) {
            Tree tree = (Tree) ids.getView(m.group(1));
            if (tree != null)
                return new DependencyTreeEngine(tree);
        }
        return null;
    }

    private static View createNodeCollectionEngine(String encoding, Graph graph, GraphIDs ids) {
        Matcher m = OsdtHelper.osdtNodeCollectionEnginePattern.matcher(encoding);
        if (m.matches()) {
            Layer nodeLayer = ids.getLayer(m.group(1));
            if (nodeLayer != null)
                return new NodeCollectionEngine(graph, nodeLayer);
        }
        return null;
    }

    private static View createProjectiveTreeEngine(String encoding, Graph graph, GraphIDs ids) {
        Matcher m = OsdtHelper.osdtProjectiveTreeEnginePattern.matcher(encoding);
        if (m.matches()) {
            NodeCollection nodeCollection = (NodeCollection) ids.getView(m.group(1));
            NodeID top = ids.getNode(m.group(2));
            Layer treeLayer = ids.getLayer(m.group(3));
            Layer siblingLayer = ids.getLayer(m.group(4));
            if (nodeCollection != null && top != null && treeLayer != null && siblingLayer != null) 
                return new ProjectiveTreeEngine(graph, nodeCollection, top, treeLayer, siblingLayer);
        }
        return null;
    }

    // 
    private static View createTreeEngine(String encoding, Graph graph, GraphIDs ids) {
        Matcher m = OsdtHelper.osdtTreeEnginePattern.matcher(encoding);
        if (m.matches()) {
            NodeCollection nodeCollection = (NodeCollection) ids.getView(m.group(1));
            NodeID top = ids.getNode(m.group(2));
            Layer treeLayer = ids.getLayer(m.group(3));
            if (nodeCollection != null && top != null && treeLayer != null)
                return new TreeEngine(graph, nodeCollection, top, treeLayer);
        }
        return null;
    }

    private static View createWordAlignmentEngine(String encoding, Graph graph, GraphIDs ids) {
        Matcher m = OsdtHelper.osdtWordAlignmentEnginePattern.matcher(encoding);
        if (m.matches()) {
            NodeCollection source = (NodeCollection) ids.getView(m.group(1));
            NodeCollection target = (NodeCollection) ids.getView(m.group(2));
            Layer alignmentLayer = ids.getLayer(m.group(3));
            if (source != null && target != null && alignmentLayer != null)
                return new WordAlignmentEngine(graph, source, target, alignmentLayer);
        }
        return null;
    }   
}
